import i18n from '@/lang'
import store from '@/store'
function checkOrg(value) {
  const tree = store.getters.orgTree ? [store.getters.orgTree] : []
  if (findObject(tree, value)) {
    return true
  } else {
    return false
  }
}
function findObject(tree, value) {
  let result = ''
  for (let i = 0; i < tree.length; i++) {
    if (tree[i].nodeKey === value) {
      result = tree[i].label
      break
    } else {
      if (tree[i].children && tree[i].children.length > 0) {
        result = findObject(tree[i].children, value)
        if (result) {
          break
        }
      }
    }
  }
  return result
}

const rules = {
  email: function(value, args) {
    const reg = /^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$/
    return {
      result: reg.test(value),
      message: i18n.t('va.email')
    }
  },
  ip: function(value, args) {
    const reg = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/
    return {
      result: reg.test(value),
      message: i18n.t('va.ip')
    }
  },
  number: function(value, args) {
    const reg = /^(-?\d+)(\.\d+)?$/
    return {
      result: reg.test(value),
      message: i18n.t('va.number')
    }
  },
  phone: function(value, args) {
    const reg = /^[0123456789-]*$/
    return {
      result: reg.test(value),
      message: i18n.t('va.phone')
    }
  },
  spChar: function(value, args) { // 特殊字符
    var reg = new RegExp("[`~!%^&|{}';'\\[\\]<>~！#￥……（）——|{}【】‘；：”“'。，、？]")
    return {
      result: !reg.test(value),
      message: i18n.t('va.spChar')
    }
  },
  upper: function(value, args) {
    const reg = /^[A-Z]+$/
    return {
      result: reg.test(value),
      message: i18n.t('va.upper')
    }
  },
  lower: function(value, args) {
    const reg = /^[a-z]+$/
    return {
      result: reg.test(value),
      message: i18n.t('va.lower')
    }
  },
  /** 数字范围  args=[min,max] */
  range: function(value, args) {
    const min = args[0]
    const max = args[1]
    const isNumber = rules.number(value, args)
    let message
    if (min === -1 && max === -1) {
      return {
        result: true
      }
    }
    if (min === -1) {
      message = i18n.t('va.range_max')
    } else if (max === -1) {
      message = i18n.t('va.range_min')
    } else {
      message = i18n.t('va.range_min_max')
    }
    message = message.replace('${max}', max).replace('${min}', min)
    if (isNumber.result === true) {
      const vNumber = Number(value)
      return {
        result: (min === -1 ? true : vNumber >= min) && (max === -1 ? true : vNumber <= max),
        message: message
      }
    } else {
      return isNumber
    }
  },
  /** 判断几位小数,[整数部分,小数部分]   -1代表不限制 */
  digits: function(value, args) {
    const isNumber = rules.number(value)
    let message
    if (isNumber.result === true) {
      const intPart = args[0] === -1 ? '' : args[0]
      const decimalPart = args[1] === -1 ? '' : args[1]

      if (args[0] === -1) {
        message = i18n.t('va.digits_decimal')
      } else if (args[1] === -1) {
        message = i18n.t('va.digits_int')
      } else {
        message = i18n.t('va.digits_int_decimal')
      }
      message = message.replace('${int}', intPart).replace('${decimal}', decimalPart)
      let reg
      if (decimalPart === 0) {
        reg = new RegExp('^(-?)\\d{1,' + intPart + '}$')
      } else {
        reg = new RegExp('^(-?)\\d{1,' + intPart + '}(\\.\\d{1,' + decimalPart + '})?$')
      }
      return {
        result: reg.test(value),
        message: message
      }
    } else {
      return isNumber
    }
  },
  /**
   * 判断长度   args=[min,max]  -1为不限制
   */
  length: function(value, args) {
    const len = value.length
    const min = args[0]
    const max = args[1]
    let message
    if (min === -1 && max === -1) {
      return {
        result: true
      }
    }
    if (min === -1) {
      message = i18n.t('va.length_max')
    } else if (max === -1) {
      message = i18n.t('va.length_min')
    } else {
      message = i18n.t('va.length_min_max')
    }
    message = message.replace('${max}', max).replace('${min}', min)
    return {
      result: (min === -1 ? true : (len >= min)) && (max === -1 ? true : (len <= max)),
      message: message
    }
  },
  /**
   * 校验是否在列表内
   */
  in: function(value, args) {
    let message = i18n.t('va.inList')
    message = message.replace('${list}', JSON.stringify(args))
    return {
      result: args.indexOf(value) !== -1,
      message: message
    }
  },
  empty: function(value, args) {
    let result
    if (value) {
      if (value.trim() === '') {
        result = false
      } else {
        result = true
      }
    } else {
      result = false
    }
    return {
      result: result,
      message: i18n.t('va.required')
    }
  },
  required: function(value, args) {
    let result
    if (value) {
      if (value.trim() === '') {
        result = false
      } else {
        result = true
      }
    } else {
      result = false
    }
    return {
      result: result,
      message: i18n.t('va.required')
    }
  },
  regExpCheck: function(value, args) {
    const reg = new RegExp(args)
    return {
      result: reg.test(value),
      message: i18n.t('va.regExpCheck')
    }
  },
  checkOrg: function(value, args) {
    return {
      result: checkOrg(value),
      message: i18n.t('va.orgNotExists')
    }
  },
  /**
   * Input only 0-9 or A-F
   */
  input09AF: function(value, args) {
    const reg = /^[0-9A-F]+$/
    return {
      result: reg.test(value),
      message: i18n.t('va.input09AF')
    }
  },
  /**
   * 限制长度只能是length   args=length
   */
  onlyLength: function(value, args) {
    const len = value.length
    const onlyLen = args
    let message = i18n.t('va.onlyLength')
    message = message.replace('${length}', onlyLen)
    return {
      result: onlyLen === len,
      message: message
    }
  }
}

export default rules
